// pages/TradingArea/GiftBagDetailHistory.js
const controller = require("../controllers/Controller").controller
const url = require("../../utils/kzj.request").url

const app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    couponBagId: "",
    detail: "",
    shopDetail: "",
    shopkey: "",
    shopMsg: "",
    show: false,
    redId: '',
    redShow: false,
    closeAnimate: false,
    isReceived: false,
    pinSuccess: false,
    canPay: true,
    baseUrl: "",
    userList:[],
    pageNumber:1,
    pageSize:8,
    lastPage:false,
    buyNum:""
  },
  //跳转店铺详情
  toShopDetail(e) {
    let shopId = e.currentTarget.dataset.shopid
    wx.navigateTo({
      url: '/pages/Index/Introduce?shopId=' + shopId,
    })
  },
  phone(e) {
    let phone = e.currentTarget.dataset.phone
    wx.makePhoneCall({
      phoneNumber: phone,
    })
  },

  //获取店铺详情
  getShopDetail(id) {
    controller.shopDetails({
      shopId: id
    }).then(res => {
      this.setData({
        shopMsg: res.data
      })
    })
  },

  //获取商圈活动用户购买列表
  getUserPayPage(){
    let value={}
    value.couponBagId=this.data.couponBagId
    value.pageNumber=this.data.pageNumber
    value.pageSize=8
    controller.getUserPayPage(value).then(res=>{

      res.data.list.forEach(item=>{
        item.nick=item.nick.split('')[0]+"***"
      })

      this.setData({
        userList:res.data.list,
        lastPage:res.data.lastPage,
        buyNum:res.data.totalRow
      })
    })
  },

  loadMore(){
    if(this.data.lastPage==false){
      let value={}
      value.pageNumber=this.data.pageNumber+1
      this.setData({
        pageNumber:value.pageNumber
      })
      value.couponBagId=this.data.couponBagId
      value.pageSize=8
      controller.getUserPayPage(value).then(res=>{
        res.data.list.forEach(item=>{
          item.nick=item.nick.split('')[0]+"***"
        })
        let list=this.data.userList
        let list2=res.data.list
        let list3=list.concat(list2)
        this.setData({
          userList:list3,
          lastPage:res.data.lastPage
        })
      })
    }
  },

  //跳转商品详情
  toGoodsDetail(e) {
    let shopId = e.currentTarget.dataset.shopid
    let id = e.currentTarget.dataset.id
    wx.navigateTo({
      url: '/pages/myCenter/shopGoodsDetail?shopId=' + shopId + "&id=" + id,
    })
  },

  //礼券包详情 2.0
  getBagActivityDetail() {
    let that = this
    controller.getActivityDetail({
      couponBagId: this.data.couponBagId
    }).then(res => {
      let list = []
      let data = res.data
      if (data.startTime) {
        data.startTime = res.data.startTime.split(" ")[0]
        data.endTime = res.data.endTime.split(" ")[0]
      }
      this.setData({
        detail: data,
        type: data.type
      })
      if (data.type == 2 || data.type == 3 || data.type == 4||data.type==5) {
        that.getShopDetail(data.shopId)
      }
      if (res.data.couponMap) {
        this.setData({
          shopkey: Object.keys(res.data.couponMap)
        })
        this.data.shopkey.forEach(item => {
          for (let i = 0; i < res.data.couponMap[item].length; i++) {
            list.push(res.data.couponMap[item][i])
          }
        })
      }

      //list分组
      let fzLength = parseInt(list.length / 2)
      let fzList = []
      let num = 0
      for (let i = 0; i < fzLength; i++) {
        let itemList = []
        itemList.push(list[num])
        itemList.push(list[num + 1])
        num = num + 2
        fzList.push(itemList)
      }
      if (list.length % 2 != 0) {
        let lzList = []
        lzList.push(list[list.length - 1])
        fzList.push(lzList)
      }


      this.setData({
        shopDetail: fzList
      })
    })

    console.log("分享参数")
    console.log(this.data.invitationAccountId)
  },


  toMyCenter() {
    this.setData({
      redShow: false,
      closeAnimate: false,
      isReceived: false
    })
    if (this.data.type == 3) {
      this.setData({
        redTimer: setInterval(function () {
          let page = getCurrentPages()
          if (page[page.length - 1].route != 'pages/TradingArea/GiftBagDetail') {
            return
          }
          that.pinList()
        }, 6000)
      })
    }
    //消除拼团成功弹出数据
    controller.readPin({
      orderNo: this.data.readPinOrderNo
    }).then(res => {
      wx.navigateTo({
        url: '/pages/myCenter/myCenter',
      })
    })
  },
  toMyAccount() {
    this.setData({
      redShow: false,
      closeAnimate: false,
      isReceived: false
    })
    if (this.data.type == 3) {
      this.setData({
        redTimer: setInterval(function () {
          let page = getCurrentPages()
          if (page[page.length - 1].route != 'pages/TradingArea/GiftBagDetail') {
            return
          }
          that.pinList()
        }, 6000)
      })
    }
    wx.navigateTo({
      url: '/pages/myCenter/myAccount',
    })
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
      let that = this
      this.setData({
        couponBagId: options.id,
        type: parseInt(options.type)
      })
      if (options.shopGroupId) {
        this.setData({
          shopGroupId: options.shopGroupId,
        })
      }
      if (options.goodsIndex) {
        this.setData({
          goodsIndex: options.goodsIndex,
        })
      } else {
        this.setData({
          goodsIndex: 0,
        })
      }
      if (options.invitationAccountId) {
        this.setData({
          invitationAccountId: options.invitationAccountId
        })
      }
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    let that = this

    if (wx.getStorageSync('usertoken')) {
      this.getBagActivityDetail()
      this.getUserPayPage()
    } else {
      app.usertokenReadyCallback = res => {
        this.getBagActivityDetail()
        this.getUserPayPage()
      }
    }
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
    let that = this
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
    let couponBagId = this.data.couponBagId
    let invitationAccountId = wx.getStorageSync('accountId')
    let title = this.data.detail.shopGroupName
    let type = this.data.type
    let url = "/pages/TradingArea/GiftBagDetail?id=" + couponBagId + "&invitationAccountId=" + invitationAccountId + "&type=" + type
    console.log(url)
    return {
      title: title,
      path: "/pages/TradingArea/GiftBagDetail?id=" + couponBagId + "&invitationAccountId=" + invitationAccountId + "&type=" + type
    }
  },
  //跳转优惠券详情
  toCouponDetail(e) {
    let id = e.currentTarget.dataset.id
    let invitationAccountId = this.data.invitationAccountId || ''
    wx.navigateTo({
      url: '/pages/TradingArea/couponDetail?id=' + id + '&couponBagId=' + this.data.couponBagId + "&invitationAccountId=" + invitationAccountId,
    })
  },

  //分享海报
  sharePoster() {
    // if (this.data.detail.isPay == false) {
    //   wx.showToast({
    //     icon: "none",
    //     title: '购买之后才能分享',
    //     duration: 1500
    //   })
    //   return
    // }
    controller.getSharePoster({
      couponBagId: this.data.couponBagId
    }).then(res => {
      this.setData({
        shareImg: res.data
      })
      let list = []
      list.push(res.data)
      wx.previewImage({
        urls: list,
      })
    })
  },

  //返回
  backBtn(e) {
    let page = getCurrentPages()
    if (page.length <= 1) {
      wx.redirectTo({
        url: '/pages/TradingArea/index',
      })
    }
  }
})